


package com.application.attachment.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.attachment.entity.Attachment;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.usertable.entity.Usertable;
import com.manager.BaseDao;
import com.manager.commons.page.PageBean;

public class AttachmentDAO extends BaseDao
{
        private static final Logger logger = Logger.getLogger(AttachmentDAO.class);

	private Attachment attachment;

	public Attachment getAttachment() {
		return this.attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	public Integer getAllCountAttachment(AttachmentForm attachmentForm){
		//储存查询条件的buffer		
		StringBuffer buf = new StringBuffer(); 
		buf.append("select count(*) from Attachment t  where  1=1");
		//查询字符串		
		String strSql = getStringBuffer(attachmentForm,buf).toString();
		
		return Integer.parseInt(this.getHibernateTemplate().find(strSql).get(0).toString());
	}
        

	public void deleteAttachments(AttachmentForm attachmentForm){    	
		StringBuffer hql = new StringBuffer(" delete from Attachment t where 1=1");
		
		Session session = this.getSession();
		try
		{
			hql = getStringBuffer(attachmentForm,hql);
			session.createQuery(hql.toString()).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deleteAttachments failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
   }
	
	public List searchAttachment(AttachmentForm attachmentForm,final PageBean pageBean){  
		StringBuffer buf = new StringBuffer();
	    
		//返回list集合
	        List list = null; 
		
		buf.append("from Attachment t  where 1=1 "); 

		//查询字符串条件		
		buf = getStringBuffer(attachmentForm,buf); 
		
		//传递查询条件buf值		
		pageBean.setStringBuffer(buf);
		
		//分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			
			public Object doInHibernate(Session session)			
					throws HibernateException, SQLException {
				
				Query query = session.createQuery(pageBean.getStringBuffer().toString()+" order by t.id Desc ");	
				
				//设置开始页
 				query.setFirstResult(pageBean.getPageSize()*(pageBean.getPageIndex()-1));
 				
 				//设置结束页
 				query.setMaxResults(pageBean.getPageSize());
 				
				List list = query.list();
				
				return list;
			}
		}
		); 
		return list;
	}
    
	public List queryAttachment(AttachmentForm attachmentForm){
		final StringBuffer hql = new StringBuffer();

		// 返回list集合
		List list = null;
        final List args = new ArrayList();
        hql.append("select a,u from Attachment a,Usertable u  where 1=1 and a.operateid = u.id ");

		 if(StringUtils.isNotBlank(attachmentForm.getFid())){
			 hql.append(" and a.fid = '" + attachmentForm.getFid() + "'");
		 }
		 if(StringUtils.isNotBlank(attachmentForm.getBak1())){
			 hql.append(" and a.bak1 = '"+ attachmentForm.getBak1()+ "'");
		 }
		 if(StringUtils.isNotBlank(attachmentForm.getType())){
			 hql.append(" and a.type = '"+ attachmentForm.getType()+ "'");
		 }
		 if(StringUtils.isNotBlank(attachmentForm.getExcept())){
			 hql.append(" and a.type not in ("+ attachmentForm.getExcept()+ ")");
		 }
		 if(StringUtils.isNotBlank(attachmentForm.getInclude())){
			 hql.append(" and a.type  in ("+ attachmentForm.getInclude()+ ")");
		 }

 

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				Query query = session.createQuery(hql.toString() + " order by a.inputtime Desc ");
				setParameter(query, args);
				List resutlList = new ArrayList();
				List teamsList = query.list();
				if(teamsList != null && teamsList.size() > 0) {
					for(int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						Attachment attachment = (Attachment) objs[0];
						Usertable usertable = (Usertable) objs[1]; 
						attachment.setRname(usertable.getRname());
						resutlList.add(attachment);
					}
				}
				return resutlList;
			}
		});
		return list;
	
	}
	
    public void updateFileNum(String id,Long num){
    	StringBuffer hql = new StringBuffer();
    	hql.append(" update yf_placefile set file_num = " + num + " where id='" + id + "'");
    	getJdbcTemplate().execute(hql.toString());
    }
	
    
   public int getFileNumCount(String id){
	   StringBuffer hql = new StringBuffer();
       hql.append(" select file_num from yf_placefile where id ='" + id + "'");
   	   return getJdbcTemplate().queryForInt(hql.toString());
   }
	/**
	 * 查询条件的封装 
	 * @param departmentForm 
	 * @return
	 */
	private StringBuffer getStringBuffer(AttachmentForm attachmentForm,StringBuffer buf){
		/* 需要加入查询条件时封装
		*/
		
           if(StringUtils.isNotBlank(attachmentForm.getFid())){
			buf.append(" and t.fid ='" + attachmentForm.getFid().trim()+"'");
		}
           if(StringUtils.isNotBlank(attachmentForm.getType())){
   			buf.append(" and t.type ='" + attachmentForm.getType().trim()+"'");
   		}
           if(attachmentForm.getId()!=null){
      			buf.append(" and t.id =" + attachmentForm.getId());
      		}
         return buf;
	}
}
	